/**
 * Created by wzh on 2016/8/10.
 */
'use strict'
swan_pc.controller("BusinessOpportunityManageController",function($scope,$rootScope,OrgCompany,OrgContact,OrgOpp,ngDialog,OppDate,$q){
    $scope.getPages();
    $scope.editItemDetail=function(id){
        OppInfo.queryAll({infoid:id},function(result){
            $scope.dates=["卖标书的日期","开标日期","宣布中标结果的日期","最终用户合同签署日期","跟我公司下单日期","后续事项处理完的日期","用户发票最晚日期","用户结帐最晚日期"]
            $scope.presentOrgOpp=result;
            $scope.orgOppPointer={
                oppBaseInfo:result.opportunity,
                oppRelatedContact:result
            }
            OppInfo.queryOppDate({infoid:id},function(result){
                $scope.orgOppPointer.oppDateList=_.keyBy(result,'name');
            })
            ngDialog.open({
                templateUrl:"scripts/components/MyOrganization/businessInfo/businessOpportunityManage/businessOpportunityDetail.html",
                scope:$scope,
                controller:["$scope",function($scope){
                    $scope.module="oppBaseInfo";
                    /*$scope.updateOrgOpp=function(module){
                        if (module=="oppBaseInfo"||module=="oppRelatedContact"){
                            OppInfo.update({},$scope.presentOrgOpp,function(){
                                alert("保存成功")
                                $scope.loadOrgOpportunity();
                                OppInfo.queryAll({infoid:id},function(result) {
                                    $scope.presentOrgOpp = result;
                                    $scope.orgOppPointer.oppBaseInfo= result.opportunity
                                    $scope.oppRelatedContact= result
                                })
                            })
                        }
                        else if (module=="oppDateList"){
                            var df=$q.defer();
                            var resolve=0;
                            _.forEach($scope.dates,function(item){
                                if ($scope.orgOppPointer.oppDateList[item]){
                                    if ($scope.orgOppPointer.oppDateList[item].id) {
                                        $scope.orgOppPointer.oppDateList[item].orgOpportunityId=$scope.presentOrgOpp.id
                                        OppDate.update($scope.orgOppPointer.oppDateList[item], function () {
                                            resolve = resolve + 1;
                                            if (resolve == 8) df.resolve()
                                        }, function () {
                                            df.reject();
                                        })
                                    }
                                    else {
                                        OppDate.save({
                                            name: item,
                                            date: $scope.orgOppPointer.oppDateList[item].date,
                                            orgOpportunityId:$scope.presentOrgOpp.id
                                        }, function () {
                                            resolve = resolve + 1;
                                            if (resolve == 8) df.resolve()
                                        }, function () {
                                            df.reject();
                                        })
                                    }
                                }
                                else {
                                    resolve=resolve+1;
                                    if (resolve==8) df.resolve()
                                }
                            })
                            df.promise.then(function(){
                                alert("保存成功")
                                OppInfo.queryOppDate({infoid:id},function(result){
                                    $scope.orgOppPointer.oppDateList=_.keyBy(result,'name');
                                })
                            },function(){
                                alert("保存失败")
                            })
                        }
                    }*/
                }]
            })
        },function(){
            $scope.presentOrgOpp=undefined;
        })
    }
})
